package com.sh169.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.nutz.log.Log;
import org.nutz.log.Logs;

import com.sh169.beans.ApplicationConfig;
import com.sh169.util.BaseHttpFilter;

/**
 * 拦截退出操作，当没有当前用户时 ，直接返回到首页
 * @author Administrator
 */
public class LogoutFilter extends BaseHttpFilter{

	private static Log log = Logs.getLog(LogoutFilter.class);
	public void init(FilterConfig filterConfig) throws ServletException {
		
		super.init(filterConfig);
	}
	private boolean requireLogout(HttpServletRequest request){
		log.debug("request uri: "+request.getRequestURI());
		log.info("logout filter: "+request.getContextPath()+ApplicationConfig.getLogout_uri());
		if(request.getRequestURI().endsWith(request.getContextPath()+ApplicationConfig.getLogout_uri()))
			return true;
		else
			return false;
	}
	public void doFilter(HttpServletRequest request,
			HttpServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		if(requireLogout(request)){
			if(request.getSession().getAttribute(SECURITY_SESSION_USER)!= null){
				request.getSession().removeAttribute(SECURITY_SESSION_USER);
			}
			response.sendRedirect(request.getContextPath()+ApplicationConfig.getIndex_uri());
			return ;
		}else{
			chain.doFilter(request, response);
		}
	}

}
